'''
Author: linxuan
Date: 2022-07-15 13:13:36
Description: 上传hdmeta的数据
FilePath: /stealer/src/hdmeta_upload.py
'''
import time
from upload import upload
import re
import json
import os

img_pattern = re.compile('<img.*?src="(.*?)".*?>')


def get_image(s: str) -> str:
    try:
        img_url = img_pattern.search(s).group(1)
    except Exception:
        img_url = ''
    return img_url


def generate_hdmeta_params(item: dict) -> dict:
    params = dict()
    params['collection'] = item['name']
    params['name'] = item['name'] + item['number']
    params['author'] = item['author']
    params['date'] = item['iso']['creation_time']
    params['amount'] = -1  # 暂时没有amount数据
    params['type'] = item['type']
    if params['type'] != '盲盒':
        params['type'] = 'picture'
    params['platform'] = 'hdmeta'
    params['price'] = item['price']
    params['token_std'] = item['iso']['iso_standards']
    params['blockchain'] = item['iso']['iso_network']
    params['content_url'] = item['cover']
    params['origin_link'] = f"https://www.hdmeta.art/shop-details.html?id={item['id']}"
    params['discription'] = get_image(item['details'])

    return params


if __name__ == '__main__':
    # 遍历/resources/hdmeta目录下的所有文件json
    for file in os.listdir('resources/hdmeta'):
        if file.endswith('.json'):
            with open(f'/resources/hdmeta/{file}'[1:], 'r') as f:
                item = json.load(f)
                params = generate_hdmeta_params(item)
                ok, err = upload(params)
                if ok != 200:
                    print("upload failed: ", err)
            time.sleep(0.1)
